//
//  SubReaderViewController.m
//  BoYue
//
//  Created by drouce on 15/11/27.
//  Copyright © 2015年 drouce. All rights reserved.
//

#import "SubReaderViewController.h"
#import "SubReaderView.h"
#import "Factory.h"
#import "TabBarViewController.h"

@interface SubReaderViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong)UILabel *titleLb;
@property (nonatomic, strong) UILabel *sourceLb;
@property (nonatomic, strong) UILabel *timeLb;
//@property (nonatomic, strong) UILabel *contentLb;
@property (nonatomic, strong) TRImageView *iconView;
@property (nonatomic, strong) UILabel *contentLb;
@property (nonatomic,assign) CGFloat screenWidth;
@end

@implementation SubReaderViewController

-(instancetype)initWithDocId:(NSString *)docId {
    if (self = [super init]) {
        self.docId = docId;
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    myLog(@"hahaha%ld",self.tabBarController.selectedIndex);
    
    //self.tabBarController.tabBar.translucent = YES;
    //self.tabBarController.tabBar.hidden = YES;
    [Factory addBackItemToVC:self];
    self.view.backgroundColor = kRGBColor(235, 235, 236);
    self.screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
    [self.sourceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLb.mas_bottom).mas_equalTo(15);
        make.leftMargin.mas_equalTo(self.titleLb.mas_leftMargin);
        
    }];
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.sourceLb.mas_right).mas_equalTo(10);
        make.topMargin.mas_equalTo(self.sourceLb.mas_topMargin);
    }];
    
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeLb.mas_bottom).mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(355);
    }];
    
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconView.mas_bottom).mas_equalTo(8);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
    
        [self.subReaderVM getDataFromNetCompleteHandle:^(NSError *error) {
            
                self.titleLb.text = [self.subReaderVM titleForRow];
                self.sourceLb.text = [self.subReaderVM sourceForRow];
                self.timeLb.text = [self.subReaderVM timeForRow];
                NSString *textStr = [self.subReaderVM contentForRow];
            
            //UILabel 显示HTML内容
            NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[textStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
            
            self.contentLb.attributedText = attrStr;
            self.contentLb.font = [UIFont systemFontOfSize:18];
            self.contentLb.backgroundColor = kRGBColor(235, 235, 236);
            
                if (self.subReaderVM.docModel.img.count) {
                   [self.iconView.imageView setImageWithURL:[self.subReaderVM iconForRow]];
                }
        }];
}





- (SubReaderViewModel *)subReaderVM {
    if(_subReaderVM == nil) {
        _subReaderVM = [[SubReaderViewModel alloc] initWithDocId:self.docId];
        
    }
        return _subReaderVM;
}


- (UILabel *)titleLb {
    if(_titleLb == nil) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.preferredMaxLayoutWidth = self.screenWidth - 30;
        [self.scrollView addSubview:_titleLb];
        _titleLb.font = [UIFont systemFontOfSize:18];
        _titleLb.numberOfLines = 0;
    }
    return _titleLb;
}

- (UILabel *)sourceLb {
    if(_sourceLb == nil) {
        _sourceLb = [[UILabel alloc] init];
        
        [self.scrollView addSubview:_sourceLb];
        
        _sourceLb.font = [UIFont systemFontOfSize:15];
        _sourceLb.textColor = [UIColor lightGrayColor];
    }
    return _sourceLb;
}

- (UILabel *)timeLb {
    if(_timeLb == nil) {
        _timeLb = [[UILabel alloc] init];
        
        [self.scrollView addSubview:_timeLb];
        
        
        _timeLb.font = [UIFont systemFontOfSize:15];
        _timeLb.textColor = [UIColor lightGrayColor];
    }
    return _timeLb;
}


- (TRImageView *)iconView {
    if(_iconView == nil) {
        _iconView = [[TRImageView alloc] init];

        [self.scrollView addSubview:_iconView];
        
    }
    return _iconView;
}


- (UIScrollView *)scrollView {
	if(_scrollView == nil) {
		_scrollView = [[UIScrollView alloc] init];
        [self.view addSubview:_scrollView];
        _scrollView.pagingEnabled = NO;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
	return _scrollView;
}

- (UILabel *)contentLb {
	if(_contentLb == nil) {
		_contentLb = [[UILabel alloc] init];
        [self.scrollView addSubview:_contentLb];
        _contentLb.numberOfLines = 0;
    
	}
	return _contentLb;
}

@end
